package com.lz.redis.common.domain;

import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: B946
 * @Date: 04/03/2020 10:28
 * @Description:
 */
public class ResponseVo {

    //结果标识 0：正常  其他：异常
    private Integer errorCode;

    //错误信息
    private String errorMsg;
    private boolean success;
    //请求参数
    private Object request;
    //返回结果
    private Object result;
    //自定义返回参数1
    private String point;
    //自定义返回参数2
    private String sole;

    public Object getRequest() {
        if (request == null) {
            return "";
        }
        return request;
    }


    public void setRequest(Object request) {
        this.request = request;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        if (errorCode != 0) {
            this.setSuccess(false);
        }
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public String getSole() {
        return sole;
    }

    public void setSole(String sole) {
        this.sole = sole;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * 使用成功表示和返回的对象创建通用json格式。
     *
     * @param success
     * @param msg
     */
    public ResponseVo(boolean success, Object msg) {
        super();
        if (success) {
            this.errorCode = 0;
            this.success = true;
            if (msg != null && StringUtils.isNotBlank(msg.toString())) {
                this.result = msg;
            }
        } else {
            this.errorCode = -1;
            this.success = false;
            if (msg != null && StringUtils.isNotBlank(msg.toString())) {
                this.errorMsg = (String) msg;
            }
        }
    }

    /**
     * 使用成功表示和返回的对象创建通用json格式。
     *
     * @param
     * @param
     */
    public ResponseVo() {
        this.errorCode = 0;
        this.success = true;
        this.errorMsg = "";
        this.result = null;
    }

    public ResponseVo(boolean success, Object... args) {
        super();

        if (success) {
            this.errorCode = 0;
            this.setSuccess(true);
            Map<String, Object> map = new HashMap<String, Object>();
            if (args != null && args.length > 0) {
                for (int i = 0; i < args.length - 1; i++) {
                    if (i % 2 == 0) {
                        if (args[i] != null && args[i + 1] != null) {
                            map.put(args[i].toString(), args[i + 1]);
                        }
                    }
                }
                this.result = map;
            }
        } else {
            this.errorCode = -1;
            this.setSuccess(false);
            Map<String, Object> map = new HashMap<String, Object>();
            if (args != null && args.length > 0) {
                for (int i = 0; i < args.length - 1; i++) {
                    if (i % 2 == 0) {
                        if (args[i] != null && args[i + 1] != null) {
                            map.put(args[i].toString(), args[i + 1]);
                        }
                    }
                }
                this.errorMsg = JSONUtil.toJsonStr(map);
            }
        }


    }

    public static ResponseVo success() {
        ResponseVo rv = new ResponseVo();
        return rv;
    }

    public static ResponseVo success(Object result) {
        ResponseVo rv = new ResponseVo();
        rv.setErrorCode(0);
        rv.setErrorMsg("");
        rv.setSuccess(true);
        rv.setResult(result);
        return rv;
    }


    public static ResponseVo failure(String msg) {
        ResponseVo rv = new ResponseVo();
        rv.setErrorCode(-1);
        rv.setSuccess(false);
        rv.setErrorMsg(msg);
        return rv;
    }

    public static ResponseVo failure(String msg, Integer code) {
        ResponseVo rv = new ResponseVo();
        rv.setErrorCode(code);
        if (code == 0) {
            rv.setSuccess(true);
        } else {
            rv.setSuccess(false);
        }
        rv.setErrorMsg(msg);
        return rv;
    }

    public static ResponseVo failure(Integer code) {
        ResponseVo rv = new ResponseVo();
        rv.setErrorCode(code);
        rv.setSuccess(false);
        rv.setErrorMsg(code + "");
        return rv;
    }

    public static ResponseVo success(Object... args) {
        if (args.length == 1) {
            return new ResponseVo(true, args[0]);
        }
        return new ResponseVo(true, args);
    }

    public static ResponseVo failure(Object... args) {
        if (args.length == 1) {
            return new ResponseVo(false, args[0]);
        }
        return new ResponseVo(false, args);
    }

    public boolean decideByErrorCode() {
        return errorCode.intValue() == 0 ? true : false;
    }
}
